package net.jeeshop.core.front;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Random;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentMap;
import net.jeeshop.core.listener.SystemListener;
import net.jeeshop.services.front.advert.bean.Advert;
import net.jeeshop.services.front.area.bean.Area;
import net.jeeshop.services.front.attribute.bean.Attribute;
import net.jeeshop.services.front.catalog.bean.Catalog;
import net.jeeshop.services.front.express.bean.Express;
import net.jeeshop.services.front.indexImg.bean.IndexImg;
import net.jeeshop.services.front.navigation.bean.Navigation;
import net.jeeshop.services.front.news.bean.News;
import net.jeeshop.services.front.notifyTemplate.bean.NotifyTemplate;
import net.jeeshop.services.front.product.bean.Product;
import net.jeeshop.services.front.product.bean.ProductStockInfo;
import net.jeeshop.services.front.systemSetting.bean.SystemSetting;
import net.jeeshop.services.manage.accountRank.bean.AccountRank;
import net.jeeshop.services.manage.activity.bean.Activity;
import net.jeeshop.services.manage.hotquery.bean.Hotquery;
import net.jeeshop.services.manage.order.bean.OrdersReport;
import net.jeeshop.services.manage.oss.bean.AliyunOSS;
import net.jeeshop.services.manage.task.bean.Task;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;
public class SystemManager {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(SystemManager.class);
    private static Properties p = new Properties();
    private static SystemManager instance = new SystemManager();
    public static List<Catalog> catalogs = new LinkedList<Catalog>();
    public static List<Catalog> catalogsArticle = new LinkedList<Catalog>();
    public static List<Attribute> attrs;
    public static Map<String,Attribute> attrsMap = new HashMap<String, Attribute>();
    public static SystemSetting systemSetting;
    public static OrdersReport ordersReport = new OrdersReport();
    public static Map<String,AccountRank> accountRankMap = new TreeMap<String, AccountRank>();
    public static Map<String,NotifyTemplate> notifyTemplateMap;
    public static List<Product> hotSearchProductList;
    public static String alipayConfig;
    public static String commentTypeCode;
    public static List<Hotquery> hotqueryList;
    public static Map<Integer, Integer> catalogMap = new HashMap<Integer, Integer>();
    public static Map<String,Catalog> catalogsMap = new HashMap<String,Catalog>();
    public static Map<String,Catalog> catalogsCodeMap = new HashMap<String,Catalog>();
    public static ConcurrentMap<String, ProductStockInfo> productStockMap; = new
    public static  Object product_stock_lock = new Object();
    public static List<List<Product>> goodsTopList;
    public static List<IndexMenu> indexMenuList;
    public static List<Navigation> navigations;
    public static List<Product> hotProducts;/
    public static List<Product> historyProducts;
    public static List<Product> newProducts;
    public static List<Product> saleProducts;
    @Deprecated
    public static List<Product> suijiProducts;
    public static List<IndexImg> indexImages;
    @Deprecated
    public static List<News> news;
    @Deprecated
    public static Map<String,News> newsMap = new HashMap<String, News>();
    public static List<Catalog> newCatalogs;
    public static List<News> noticeList;
    public static Map<String, Area> areaMap = new HashMap<String, Area>();
    public static Map<String,Express> expressMap;
    public static Map<String,Advert> advertMap;
    public static Map<String,String> manageExpressMap = new HashMap<String, String>();
    public static AliyunOSS aliyunOSS;
    public static Task task;/
    public static List<Product> indexLeftProduct;
    public static Map<String,Activity> activityMap = new HashMap<String, Activity>();
    public static Map<String,List<Product>> activityProductMap = new HashMap<String, List<Product>>();
    public static List<Product> activityScoreProductList;
    public static List<Product> activityTuanProductList;
    public static String productCatalogJsonStr;
    public static String articleCatalogJsonStr;
    static {
    }
    public static SystemManager getInstance(){
        return instance;
    }
    private static void setBucketPublicReadable(OSSClient client, String bucketName)
            throws OSSException, ClientException {
        client.createBucket(bucketName);
        client.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
    }
    class NameComparator implements Comparator<Object> {
        public int compare(Object a, Object b) {
            Hashtable hashA = (Hashtable)a;
            Hashtable hashB = (Hashtable)b;
            if (((Boolean)hashA.get("is_dir")) && !((Boolean)hashB.get("is_dir"))) {
                return -1;
            } else if (!((Boolean)hashA.get("is_dir")) && ((Boolean)hashB.get("is_dir"))) {
                return 1;
            } else {
                return ((String)hashA.get("filename")).compareTo((String)hashB.get("filename"));
            }
        }
    }
    class SizeComparator implements Comparator<Object> {
        public int compare(Object a, Object b) {
            Hashtable hashA = (Hashtable)a;
            Hashtable hashB = (Hashtable)b;
            if (((Boolean)hashA.get("is_dir")) && !((Boolean)hashB.get("is_dir"))) {
                return -1;
            } else if (!((Boolean)hashA.get("is_dir")) && ((Boolean)hashB.get("is_dir"))) {
                return 1;
            } else {
                if (((Long)hashA.get("filesize")) > ((Long)hashB.get("filesize"))) {
                    return 1;
                } else if (((Long)hashA.get("filesize")) < ((Long)hashB.get("filesize"))) {
                    return -1;
                } else {
                    return 0;
                }
            }
        }
    }
    private static void init(){
        try {
            p.load(SystemListener.class
                    .getResourceAsStream("/system.properties"));
            code.load(new BufferedReader(new InputStreamReader(SystemListener.class
                    .getResourceAsStream("/code.properties"), "utf-8")));
            logger.info(p.toString());
            log.info(code.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public String get(String key){
        return p.getProperty(key);
    }
    private Random random = new Random();
    public String getImageRandom(){
        if(systemSetting==null || systemSetting.getImagesList()==null ||
                systemSetting.getImagesList().size()==0){
            return null;
        }
        int n = random.nextInt(systemSetting.getImagesList().size());
        return systemSetting.getImageRootPath()+systemSetting.getImagesList().get(n);
    }
    public static String getContextPath(HttpServletRequest request){
        if(Boolean.valueOf(getInstance().get("is_www"))){
            return SystemManager.getInstance().get("contextPath");
        }
        return request.getContextPath();
    }
    public List<String> getCatalogsById(String catalogID) {
        if(org.apache.commons.lang.StringUtils.isBlank(catalogID)){
            return null;
        }
        if(this.catalogs==null || this.catalogs.size()==0){
            return null;
        }
        List<String> list = new LinkedList<String>();
        Catalog cataInfo = catalogsMap.get(catalogID);
        if(cataInfo.getPid().equals("0")){
            for(int i=0;i<cataInfo.getChildren().size();i++){
                list.add(cataInfo.getChildren().get(i).getId());
            }
        }else{
            list.add(catalogID);
        }
        return list;
    }
    public List<Product> getProductsByCatalogCode(String catalogCode){
        logger.error("getProductsByCatalogCode.catalogCode = " + catalogCode);
        if(StringUtils.isNotBlank(catalogCode)){
            Catalog catalog = SystemManager.catalogsCodeMap.get(catalogCode);
            if(catalog!=null){
                if(catalog.getHotProducts()==null || catalog.getHotProducts().size()==0){
                    logger.error("catalog.getHotProducts()=0");
                }else{
                    logger.error("catalog.getHotProducts()="+catalog.getHotProducts().size());
                }
                return catalog.getHotProducts();
            }
        }
        return SystemManager.indexLeftProduct;
    }
}